package it.mauriziobasile.dietarapida.gui;


import java.util.ArrayList;
import java.util.Properties;

import javax.swing.JFrame;
import javax.swing.table.DefaultTableModel;

/**
 * 
 * @author Maurizio Basile
 */
public class SelectNutrienti extends javax.swing.JFrame {

    private static final long serialVersionUID = -7124917983432458437L;

    private Properties properties;

    public static String[] proNames = { "nutriSelezione", "nutriPasti", "nutriRiassunto" };

    int lastSelected = 0;

    private DietaRapidaView parent;

    public SelectNutrienti(DietaRapidaView par) {
        
        parent = par;
        initComponents();
        properties = parent.getProperties();
        if (properties.containsKey(proNames[lastSelected])) {
            String[] nutri = properties.getProperty(proNames[lastSelected]).split(";");
            ArrayList<String> nutrisel = new ArrayList<String>();
            for (int i = 0; i < nutri.length; i++) {
                nutrisel.add(nutri[i]);
            }

            String[] allnutri = properties.getProperty("tuttecolonne").split(";");
            for (int i = 0; i < allnutri.length; i++) {
                if (!allnutri[i].contains("ALI_DESC"))
                    ((DefaultTableModel) jTable1.getModel()).addRow(new Object[] { nutrisel.contains(allnutri[i]), allnutri[i] });
            }
        }
        setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE);
    }

  
    private void initComponents() {

        pasti = new javax.swing.JComboBox();
        jScrollPane1 = new javax.swing.JScrollPane();
        jTable1 = new javax.swing.JTable();
        jButtonChiudi = new javax.swing.JButton();
        setName("Form"); 
        pasti.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Selezione", "Pasti", "Riassunto" }));
        pasti.setName("pasti"); // NOI18N
        pasti.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                pastiActionPerformed(evt);
            }
        });

        jScrollPane1.setName("jScrollPane1");

        jTable1.setModel(new javax.swing.table.DefaultTableModel(new Object[][] {

        }, new String[] { "x", "Nutriente" }) {

            private static final long serialVersionUID = 8206895832074934259L;
            Class<?>[] types = new Class[] { java.lang.Boolean.class, java.lang.String.class };
            boolean[] canEdit = new boolean[] { true, false };

            public Class<?> getColumnClass(int columnIndex) {
                return types[columnIndex];
            }

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit[columnIndex];
            }
        });
        jTable1.setName("jTable1");
        jTable1.getTableHeader().setReorderingAllowed(false);
        jScrollPane1.setViewportView(jTable1);
        jTable1.getColumnModel().getColumn(0).setResizable(false);
        jTable1.getColumnModel().getColumn(0).setHeaderValue("X");
        jTable1.getColumnModel().getColumn(1).setResizable(false);
        jTable1.getColumnModel().getColumn(1).setHeaderValue("");
        jTable1.getColumnModel().getColumn(0).setWidth(15);
        jTable1.getColumnModel().getColumn(0).setMinWidth(15);
        jTable1.getColumnModel().getColumn(0).setMaxWidth(15);

        jButtonChiudi.setText("Chiudi");
        jButtonChiudi.setName("jButtonChiudi");
        jButtonChiudi.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonChiudiActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).addGroup(layout.createSequentialGroup().addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).addGroup(layout.createSequentialGroup().addGap(143, 143, 143).addComponent(pasti, javax.swing.GroupLayout.PREFERRED_SIZE, 154, javax.swing.GroupLayout.PREFERRED_SIZE)).addGroup(layout.createSequentialGroup().addContainerGap().addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 510, Short.MAX_VALUE))).addContainerGap()).addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup().addContainerGap(407, Short.MAX_VALUE).addComponent(jButtonChiudi).addGap(30, 30, 30)));
        layout.setVerticalGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).addGroup(layout.createSequentialGroup().addGap(15, 15, 15).addComponent(pasti, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE).addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED).addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 375, javax.swing.GroupLayout.PREFERRED_SIZE).addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED).addComponent(jButtonChiudi).addContainerGap(10, Short.MAX_VALUE)));

        pack();
    }

    private void jButtonChiudiActionPerformed(java.awt.event.ActionEvent evt) {
        storeNutrientiProp();
        parent.setProperties(properties);
        parent.formatTables();
        this.setVisible(false);
    }

    private void pastiActionPerformed(java.awt.event.ActionEvent evt) {// GEN-FIRST:event_pastiActionPerformed
        int pasto = pasti.getSelectedIndex();
        storeNutrientiProp();
        loadNutrientiProp(proNames[pasto]);
        lastSelected = pasto;
    }

    private void storeNutrientiProp() {
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < jTable1.getRowCount(); i++) {
            if (jTable1.getValueAt(i, 0).equals(true)) {
                sb.append(jTable1.getValueAt(i, 1));
                sb.append(";");
            }
        }
        properties.setProperty(proNames[lastSelected], sb.toString());

    }

    private void loadNutrientiProp(String name) {
        if (properties.containsKey(name)) {
            String[] nutri = properties.getProperty(name).split(";");
            ArrayList<String> nutrisel = new ArrayList<String>();
            for (int i = 0; i < nutri.length; i++) {
                nutrisel.add(nutri[i]);
            }
            for (int i = 0; i < jTable1.getRowCount(); i++) {
                if (nutrisel.contains(jTable1.getValueAt(i, 1)))
                    jTable1.setValueAt(true, i, 0);
                else
                    jTable1.setValueAt(false, i, 0);
            }
        } else {
            properties.setProperty(name, properties.getProperty("nutrienti"));
        }
    }

    private javax.swing.JButton jButtonChiudi;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTable jTable1;
    private javax.swing.JComboBox pasti;

}
